package com.company.pdd;

import java.util.Arrays;
import java.util.Scanner;

public class Main3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long M = sc.nextLong();
        long []player = new long[N];
        for(int i = 0;i < N;i++){
            long num = sc.nextLong();
            player[i] = num;
        }
        long res = least(player,N,M);
        System.out.println(res);
        sc.close();

    }

    private static long least(long[] player,int N,long M){
        long res = 0;
        Arrays.sort(player);

        int i = 0;
        int j = N - 1;
        while (i < j && M > 0){
            if(player[j] - player[i] > 1){
                player[j] --;
                player[i]++;
                res++;
                if(player[i] > player[i+1]){
                    swap(player,i,i+1);

                }
                if(player[i] == player[i+1]){
                    i++;
                }
                if(player[j]   > player[j-1]){
                   swap(player,j,j-1);
                }
                if(player[j] == player[j-1]){
                    j--;
                }
            }

            M--;
        }
        return res;

    }

    private static void swap(long[] player,int i,int j ){
        long temp = player[i];
        player[i] = player[j];
        player[j] = temp;
    }
}
